/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Participant;
import entity.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import security.EventBean;


/**
 *
 * @author MONESI
 */

@Stateless
@Named
public class InviteManager {
    
    @PersistenceContext
    EntityManager em;
    
   
      
    @Inject
    Principal principal;
    List<Integer> acceptedinvitedid;
    List<Integer> listAcceptedEventsId;
    List<String> listAcceptedEvents;
    List<Integer> save;
    List<String> invited;
    List<Integer> n;
    List<Integer> id;
    List<Integer> invitedId;
       
  
    public void save(Participant participant) {
        
        save = em.createQuery("SELECT e.eventId FROM EVENTS e ").getResultList();
        participant.setEventid(save.get(save.size()-1));
        em.persist(participant);
        
    }
    
    public List<Event> creaEventiInvitato(String loggedUser){
        return em.createQuery("SELECT e FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId = e.eventId AND p.outcome = 1").setParameter("logged", loggedUser).getResultList();                   

    }
    
    public List<Integer> numberOfEvents (String loggedUser){
         n = em.createQuery("SELECT COUNT(p.eventId) FROM PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.outcome = 0").setParameter("logged", loggedUser).getResultList();
         return n;
    }
    
    
    public List<String> Invited(String loggedUser) {
        //per notifica letta o non letta basta mettere p.letta=0. Anche per contare quante non sono state lette

        invited = em.createQuery("SELECT e.name FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId = e.eventId AND p.outcome = 0").setParameter("logged", loggedUser).getResultList();                   
        
        //Query q = em.createQuery("UPDATE PARTICIPANT SET letta=1 WHERE eventId = 1");                   

        return invited;        
    }
    
    public List<String> listAcceptedEvents(String loggedUser) {
        //per notifica letta o non letta basta mettere p.letta=0. Anche per contare quante non sono state lette

        listAcceptedEvents = em.createQuery("SELECT e.name FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId = e.eventId AND p.outcome = 1 AND e.cancella=0").setParameter("logged", loggedUser).getResultList();                   
        
        //Query q = em.createQuery("UPDATE PARTICIPANT SET letta=1 WHERE eventId = 1");                   

        return listAcceptedEvents;        
    }
    
     public List<Integer> listAcceptedEventsId(String loggedUser) {
        //per notifica letta o non letta basta mettere p.letta=0. Anche per contare quante non sono state lette

        listAcceptedEventsId = em.createQuery("SELECT e.eventId FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId = e.eventId AND p.outcome = 1 AND e.cancella=0").setParameter("logged", loggedUser).getResultList();                   
        
        //Query q = em.createQuery("UPDATE PARTICIPANT SET letta=1 WHERE eventId = 1");                   

        return listAcceptedEventsId;        
    }
     
    public List<String> AcceptedEvent(String loggedUser, Date data) {
        //per notifica letta o non letta basta mettere p.letta=0. Anche per contare quante non sono state lette

        invited = em.createQuery("SELECT e.name FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND e.date LIKE :date AND p.eventId = e.eventId AND p.outcome = 1 AND e.cancella=0").setParameter("logged", loggedUser).setParameter("date", data).getResultList();                   
        
        //Query q = em.createQuery("UPDATE PARTICIPANT SET letta=1 WHERE eventId = 1");                   

        return invited;
        
           
    }
    
    public List<Integer> acceptedEventId(String loggedUser, Date data) {
        //per notifica letta o non letta basta mettere p.letta=0. Anche per contare quante non sono state lette

        acceptedinvitedid = em.createQuery("SELECT e.eventId FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND e.date LIKE :date AND p.eventId = e.eventId AND p.outcome = 1 AND e.cancella=0").setParameter("logged", loggedUser).setParameter("date", data).getResultList();                   
        
        //Query q = em.createQuery("UPDATE PARTICIPANT SET letta=1 WHERE eventId = 1");                   

        return acceptedinvitedid;
        
           
    }
    
    public List <String> partecipanti(int id){
        List<String> participant = em.createQuery("SELECT p.userInvited FROM PARTICIPANT p WHERE p.eventId LIKE :id AND p.outcome = 1").setParameter("id", id).getResultList();                   
            return participant;
    }
    
    public List <String> invitati(int id){
        List<String> participant = em.createQuery("SELECT p.userInvited FROM PARTICIPANT p WHERE p.eventId LIKE :id AND p.outcome = 0").setParameter("id", id).getResultList();                   
            return participant;
    }
    
    public List<Date> giorniInvitato(String loggedUser){

        return em.createQuery("SELECT e.date FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId = e.eventId AND p.outcome = 1 GROUP BY e.date").setParameter("logged", loggedUser).getResultList();
    }
    
    public List<Integer> InvitedId(String loggedUser) {
        
        invitedId = em.createQuery("SELECT e.eventId FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId = e.eventId AND p.outcome = 0").setParameter("logged", loggedUser).getResultList();                   
        
        return invitedId;        
    }
    
    
    public void azzera(String loggedUser) {
        //Azzera il numero di notifiche non lette
        //<a:commandButton class="btn btn-primary btn-lg" id="submit1" value="Azzera" action="#{participantBean.azzeraNotifiche()}" />                      
        //sopra il codice da incorporare per cancellare le notifiche non lette
        Query q = em.createQuery("UPDATE PARTICIPANT p SET p.letta=1 WHERE p.userInvited LIKE :loggedUser").setParameter("loggedUser", loggedUser);
        q.executeUpdate();
       
        
    }
    public void acceptEventById(String loggedUser, int idpar) {
        Query q = em.createQuery("UPDATE PARTICIPANT p SET p.outcome=1 WHERE p.userInvited LIKE :loggedUser AND p.eventId LIKE :id").setParameter("loggedUser", loggedUser).setParameter("id", idpar);
        q.executeUpdate();

        //return id = em.createQuery("SELECT e.eventId FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId LIKE :id AND p.eventId = e.eventId").setParameter("logged", loggedUser).setParameter("id", idpar).getResultList();                   
            
    }
    
    public void declineEventById(String loggedUser, int idpar) {
        Query q = em.createQuery("UPDATE PARTICIPANT p SET p.outcome=2 WHERE p.userInvited LIKE :loggedUser AND p.eventId LIKE :id").setParameter("loggedUser", loggedUser).setParameter("id", idpar);
        q.executeUpdate();

        //return id = em.createQuery("SELECT e.eventId FROM EVENTS e JOIN PARTICIPANT p WHERE p.userInvited LIKE :logged AND p.eventId LIKE :id AND p.eventId = e.eventId").setParameter("logged", loggedUser).setParameter("id", idpar).getResultList();                   
            
    }
    
    public User getLoggedUser() {
        return em.find(User.class, principal.getName());
    }
    
}

